﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BusinessLogic;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;

namespace DataAccessLogic
{
    public static class CodeTableDataAccess
    {
        #region Methods
        public static T BindRowToCodeTable<T>(DataRow row) where T : CodeTable<T>, new()
        {
            T t = null;

            if(row != null)
            {
                t = new T();
                t.ID = DataAccess.CheckForGuidColumn(row, "ID");
                t.Description = DataAccess.CheckForStringColumn(row, "DESCRIPTION");
                t.DateAdded = DataAccess.CheckForDateTimeColumn(row, "DATEADDED");
                t.DateChanged = DataAccess.CheckForDateTimeColumn(row, "DATECHANGED");
                t.AddedByID = DataAccess.CheckForGuidColumn(row, "ADDEDBYID");
                t.ChangedByID = DataAccess.CheckForGuidColumn(row, "CHANGEDBYID");
                t.AddedBy = DataAccess.CheckForStringColumn(row, "ADDEDBY");
                t.ChangedBy = DataAccess.CheckForStringColumn(row, "CHANGEDBY");                
            }

            return t;
        }

        public static Guid Create<T>(string storedProcedure, string description) where T : CodeTable<T>, new()
        {
            Guid id = Guid.Empty;
            Guid providerUserKey = new Guid(Membership.GetUser().ProviderUserKey.ToString());
            string outputParameterName = "@ID";

            if (!string.IsNullOrWhiteSpace(description))
            {
                SqlParameter p = new SqlParameter();
                p.ParameterName = outputParameterName;
                p.Direction = ParameterDirection.Output;
                p.DbType = DbType.Guid;

                List<SqlParameter> parms = new List<SqlParameter>();
                parms.Add(p);
                parms.Add(new SqlParameter("@DESCRIPTION", description));
                parms.Add(new SqlParameter("@ASPNETDBUSERID", providerUserKey));

                object obj = DataAccess.Create(storedProcedure, outputParameterName, parms);

                if (obj != null)
                {
                    id = new Guid(obj.ToString());

                    if(id != Guid.Empty)
                    {
                        MyCache.RefreshCodeTableCache<T>(new T().GetAll());
                    }
                }
            }            

            return id;
        }

        public static List<Exception> Delete<T>(string storedProcedure, Guid id) where T : CodeTable<T>, new()
        {
            List<Exception> exes = null; 

            if (id != Guid.Empty)
            {
                exes = DataAccess.Delete(storedProcedure, id).Exceptions;
                MyCache.RefreshCodeTableCache<T>(new T().GetAll());                
            }

            return exes;
        }

        public static DataTable GetAll(string storedProcedure)
        {
            return DataAccess.Get(storedProcedure);
        }

        public static DataRow GetByID(string storedProcedure, Guid id)
        {
            DataRow dr = null;

            if(id != Guid.Empty)
            {
                List<SqlParameter> parms = new List<SqlParameter>();
                parms.Add(new SqlParameter("@ID", id));
            
                DataTable dt = DataAccess.Get(storedProcedure, parms);
            
                if((dt != null) && (0 < dt.Rows.Count))
                {
                    dr = dt.Rows[0];
                }
            }

            return dr;
        }

        public static void Update<T>(string storedProcedure, Guid id, string description) where T : CodeTable<T>, new()
        {
            Guid providerUserKey = new Guid(Membership.GetUser().ProviderUserKey.ToString());

            if ((id != Guid.Empty) && (!string.IsNullOrWhiteSpace(description)))
            {
                List<SqlParameter> parms = new List<SqlParameter>();
                parms.Add(new SqlParameter("@ID", id));
                parms.Add(new SqlParameter("@DESCRIPTION", description));
                parms.Add(new SqlParameter("@ASPNETDBUSERID", providerUserKey));

                DataAccess.Update(storedProcedure, parms);
                
                MyCache.RefreshCodeTableCache<T>(new T().GetAll());
            }
        }
        #endregion
    }
}